

$(function () {
    var xsdId = !$("[name='xsd.id']").val() ? 0 : $("[name='xsd.id']").val();
    xsdmxGrid($.webPath() + '/xsgl/xsdgl/xsdgl_xsmx?xsdId=' + xsdId);
    xsdfyGrid($.webPath() + '/xsgl/xsdgl/xsdgl_xsfy?xsdId=' + xsdId);

})


function xsdmxGrid(href) {
    $("#xsdtz_mx").edatagrid({
        url: href,
        fitColumns: false,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        showFooter: true,
        toolbar: "#xsdgl_xsdmx_toolbar",
        frozenColumns: [
            [
                {field: 'checked', checkbox: true},
                {field: 'wzgg.id', title: '物资编码ID', hidden: true},
                {field: 'wzgg.oldcode', title: '老物资编码', width: 90},
                {field: 'wzgg.code', title: '物资编码', width: 90},
                {field: 'wzgg.name', title: '物资名称', width: 50},
                {field: 'wzgg.ggxh', title: '规格型号', width: 85},
                {field: 'wzgg.th', title: '图号', width: 120},
                {field: 'wzgg.dw', title: '单位', width: 30},
                {field: 'jzfs.z', title: '记重方式', width: 50}
            ]
        ],
        columns: [
            [
                {field: 'kh.khbm', title: '卡号', width: 130},
                {field: 'xscb', title: '库存单价',formatter: function (value) {
                    var value = formatJeForHtml(value, ",");
                    return value;
                },  width: 70},
                {field: 'js', title: '件数', width: 70},
                {field: 'sbsl', title: '数量', editor: {type: 'numberbox', options: {min: 0, precision: 4}},
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 100},
                {field: 'hsj', title: '单价(含税)',
                    formatter: function (value) {
                        var value = formatJeForHtml(value, ",");
                        return value;
                    }, width: 100},
                {field: 'bhsj', title: '单价(不含税)',
                    formatter: function (value) {
                        var value = formatJeForHtml(value, ",");
                        return value;
                    }, width: 100},
                {field: 'hse', title: '含税额(元)',
                    formatter: function (value) {
                        var value = formatJeForHtml2(value, ",");
                        return value;
                    }, width: 100},
                {field: 'bhse', title: '无税额(元)',
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value, ",");
                        return sl;
                    }, width: 100}
            ]
        ],
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.bhsj) {
                    this.bhse = this.bhsj * this.sbsl;
                }
                if (this.hsj) {
                    this.hse = this.hsj * this.sbsl;
                }
                this.sbsl = 0;
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            //获取每个单元格
            $("#xsdtz_mx").edatagrid('editRow', rowIndex);
            var editors = $("#xsdtz_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
        },
        onLoadSuccess: function () {
            countSumXsd();
        }
    })
}


function xsdfyGrid(href) {
    $("#xsdtz_fy").edatagrid({
        url: href,
        fitColumns: true,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        rownumbers: true,
        toolbar: "#xsdgl_xsdfy_toolbar",
        columns: [
            [
                {field: 'checked', checkbox: true},
                {field: 'sydw.z', title: '收益单位', width: 50},

                {field: 'fylb.fykmjc', title: '费用类别', width: 30},
                {field: 'jfsl', title: '计费数量', width: 40},
                {field: 'dj', title: '单价(元)',formatter: function (value) {
                    var value = formatJeForHtml(value, ",");
                    return value;
                }, width: 40},
                {field: 'je', title: '金额(元)',formatter: function (value) {
                    var value = formatJeForHtml2(value, ",");
                    return value;
                }, width: 50},
                {field: 'bz', title: '备注', width: 50}
            ]
        ]
    })
}


//合计计算
function countSumXsd() {
    var sumxscb = 0;
    var sumjs = 0;
    var sumsbsl = 0;
    var sumhse = 0;
    var sumbhse = 0;
    var data = $("#xsdtz_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var xscb = parseFloat(data.rows[i]["xscb"]);
        var js = parseFloat(data.rows[i]["js"]);
        var sbsl = parseFloat(data.rows[i]["sbsl"]);
        var hse = parseFloat(data.rows[i]["hse"]);
        var bhse = parseFloat(data.rows[i]["bhse"]);
        xscb = isNaN(xscb) ? 0 : xscb;
        js = isNaN(js) ? 0 : js;
        sbsl = isNaN(sbsl) ? 0 : sbsl;
        hse = isNaN(hse) ? 0 : hse;
        bhse = isNaN(bhse) ? 0 : bhse;
        sumxscb += xscb;
        sumjs += js;
        sumsbsl += sbsl;
        sumhse += hse;
        sumbhse += bhse;
    }
    addFooter(sumxscb, sumjs, sumsbsl, sumhse, sumbhse);

}

function addFooter(sumxscb, sumjs, sumsbsl, sumhse, sumbhse) {
    var footer = null;
    footer = [
        {"wzgg.code": "合计", "xscb": formatJe(sumxscb), "js": formatSl(sumjs),
            "sbsl": formatSl(sumsbsl), "hse": formatJe(sumhse), "bhse": formatJe(sumbhse)}
    ];
    if (footer != null)
        $("#xsdtz_mx").edatagrid('reloadFooter', footer);
}
